package com.itboye.sunsun.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.TieZiBeanNew;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.custome.FlowLayout;
import com.itboye.sunsun.custome.RoundImage;
import com.itboye.sunsun.utils.DateLineParse;
import com.itboye.sunsun.utils.DensityUtil;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.String2Spanned;
import com.itboye.sunsunhome.R;
import com.pobing.uilibs.extend.component.dialog.ImageViewerDialog;

public class LuntanZhiKanLouZhuAdapter extends BaseAdapter {

	 private Activity context;
	private ArrayList<TieZiBeanNew.TieZiCommentItem> list;
	OnClickListener commentListener;

	public LuntanZhiKanLouZhuAdapter(
			ArrayList<TieZiBeanNew.TieZiCommentItem> list,
			OnClickListener commentListener,Activity context) {
		this.list = list;
		this.commentListener = commentListener;
		this.context=context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return list == null ? 0 : list.size();

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list == null ? 0 : position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(App.ctx).inflate(
					R.layout.item_luntan_zhikanlouzhu, null);

			viewHolder = new ViewHolder();
			viewHolder.head = (RoundImage) convertView.findViewById(R.id.head);
			viewHolder.txt_reply = (TextView) convertView
					.findViewById(R.id.txt_reply);
			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.txt_floor = (TextView) convertView
					.findViewById(R.id.txt_floor);
			viewHolder.txt_time = (TextView) convertView
					.findViewById(R.id.txt_time);
			viewHolder.re_comment = (RelativeLayout) convertView
					.findViewById(R.id.re_comment);
			viewHolder.comment_content = (TextView) convertView
					.findViewById(R.id.comment_content);
			viewHolder.yuancomment = (TextView) convertView
					.findViewById(R.id.yuancomment);
			viewHolder.re_huifu = (RelativeLayout) convertView
					.findViewById(R.id.re_huifu);
			viewHolder.flow_imgContainer = (FlowLayout) convertView
					.findViewById(R.id.flow_imgContainer);
//			viewHolder.txt_timelouzhu=(TextView) convertView.findViewById(R.id.txt_timelouzhu);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
//		viewHolder.txt_timelouzhu.setVisibility(View.VISIBLE);
		
		viewHolder.txt_time.setText(DateLineParse.getParseTime(list.get(
				position).getDateline()));

		viewHolder.comment_content.setText(String2Spanned.parsed(list.get(
				position).getcComment()));
		XImageLoader.load(list.get(position).getHead(), viewHolder.head);
		viewHolder.txt_floor.setText("第" + (position) + "楼");
		if (list.get(position).getType().equals("1")) {// 回复
			viewHolder.yuancomment.setVisibility(View.GONE);
			viewHolder.re_huifu.setVisibility(View.GONE);
			viewHolder.name.setText(list.get(position).getAuthor());
			viewHolder.comment_content.setText(String2Spanned.parsed(list.get(
					position).getMessage()));
		} else if (list.get(position).getType().equals("2")) {
			viewHolder.name.setText(list.get(position).getcAuthor());
			viewHolder.re_huifu.setVisibility(View.VISIBLE);
			viewHolder.yuancomment.setVisibility(View.VISIBLE);// 评论
			viewHolder.yuancomment.setText("回复"
					+ String2Spanned.parsed(list.get(position).getAuthor())
					+ ":" + list.get(position).getMessage());
		}
		final List<String> imgs = list.get(position).getImgId();

		int width = DensityUtil.dip2px(80);
		int height = DensityUtil.dip2px(100);
		viewHolder.flow_imgContainer.removeAllViews();
		if (list.get(position).getImgId() != null) {
			if (list.get(position).getImgId().size() > 0) {
				for (int i = 0; i < list.get(position).getImgId().size(); i++) {
					ImageView imageView = new ImageView(context);
					imageView.setScaleType(ScaleType.FIT_XY);
					imageView.setTag(i);
					// ViewGroup.MarginLayoutParams layoutParams = new
					// ViewGroup.MarginLayoutParams(
					// width, height);
					android.widget.LinearLayout.LayoutParams layoutParams2 = new android.widget.LinearLayout.LayoutParams(
							width, height);
					// layoutParams2.set
					layoutParams2.setMargins(5, 5, 5, 5);
					XImageLoader.load(NetPublicConstant.IMAGE_URL + list.get(position).getImgId().get(i),
							imageView);
					viewHolder.flow_imgContainer.addView(imageView,
							layoutParams2);
					
					imageView.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
								ImageViewerDialog imageViewerDialog = new ImageViewerDialog(
										context);
								String urls[] = new String[imgs.size()];
								for (int i = 0; i < urls.length; i++) {
									urls[i] = NetPublicConstant.IMAGE_URL + imgs.get(i);
								}
								imageViewerDialog.setImageUrls(urls);
								imageViewerDialog.show((int)v.getTag());
//							}
		//
							
							
							
						}
					});
				}
				
				viewHolder.flow_imgContainer.setVisibility(View.VISIBLE);
			} else {
				viewHolder.flow_imgContainer.setVisibility(View.GONE);
			}
		}

		viewHolder.re_comment.setTag(list.get(position).getAuthorId());
		viewHolder.re_comment.setTag(R.id.tag_first,list.get(position).getAuthor());
		viewHolder.re_comment.setTag(R.id.tag_second,list.get(position).getPid());
		viewHolder.re_comment.setOnClickListener(commentListener);

		return convertView;
	}

	class ViewHolder {
		FlowLayout flow_imgContainer;

		RoundImage head;

		TextView name, yuancomment;
		TextView txt_floor;
		TextView txt_time;
		TextView txt_reply;
		TextView comment_content;
		
		TextView txt_timelouzhu;
		RelativeLayout re_comment, re_huifu;
	}

}
